<?php

/**
*Form Builder & Admin Generator

*@author marcio <opi14@op.pl>, <polishvodka7@gmail.com>
*@copyright Copyright (c) 2011, marcio
*@version 1.5.2
*/

class Validation 
{

	protected $type;

	protected $rules = array();
  
	protected $msg;
  
	private $lang = null;
  
  
	public function __construct($type = '') 
	{
  
		$this -> lang = new Language;
		$this -> lang -> setLangMessage(get_class($this));
  
		if(isset($type) && !empty($type))
			$this -> type = $type;

		else
			$this -> type = 'post';
  
	}
  
  
	protected function getTypeValidation() 
	{
		return $this -> type;
	}
  
  
	public function Submitted($submit) 
	{
  
		$submitType = ($this -> getTypeValidation() == 'post') ? $_POST : $_GET;
  
		if(!isset($submitType[$submit]))
			return false;
		
		else
			return true;
  
	}
  
  
	public function AddRules($fieldname, $label, $rules = array()) 
	{
  
		if(empty($label)) 
			$label = $fieldname;
	  
		if(empty($fieldname))
			$fieldname = $label;
	  
		$submitType = ($this -> getTypeValidation() == 'post') ? $_POST : $_GET;
	  
		$this -> rules[$fieldname][] = array($submitType[$fieldname] => $label.'|'.$rules);
	  
	}
  
  
	public function Validation() 
	{
  
		foreach($this -> rules as $rule => $keyRule) //login i password
		{ 
	  
			foreach($keyRule as $post => $validRule) 
			{
		
				foreach($validRule as $field => $rules) 
				{
		
					$methodRule = explode('|', $rules);
		
						switch($methodRule[1]) 
						{
		
							case 'required':
		  
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
						         if(!$this -> $methodRule[1]($field))
						             $this -> msg[] =  $this -> setError('required', $methodRule[0]);
								 
								}
		  
							break;
			
							case 'MinLenght':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field, $methodRule[2]))
										$this -> msg[] =  $this -> setError('MinLenght', $methodRule[0]);
						  
								}                
			
							break;
			
							case 'MaxLenght':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field, $methodRule[2]))
						             $this -> msg[] =  $this -> setError('MaxLenght', $methodRule[0]);
						  
								}                
			
							break;
			
							case 'Between':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field, $methodRule[2], $methodRule[3]))
										$this -> msg[] =  $this -> setError('Between', $methodRule[0]);
									 
								} 
			
							break;
			
							case 'Email':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field)) 
										$this -> msg[] =  $this -> setError('Email', $methodRule[0]);
	
								} 
			
							break;
			
							case 'Ip':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field))
										$this -> msg[] =  $this -> setError('Ip', $methodRule[0]);
						  
								} 
			
							break;
			
							case 'Regexp':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field, $methodRule[2]))
										$this -> msg[] =  $this -> setError('Regexp', $methodRule[0]);
						  
								} 
			
							break;
			
							case 'Alpha':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field))
										$this -> msg[] =  $this -> setError('Alpha', $methodRule[0]);

								} 
			
							break;
			
							case 'Digit':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field)) 
										$this -> msg[] =  $this -> setError('Digit', $methodRule[0]);
						  
								} 
			
							break;
			
							case 'AlphaDigit':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field))
										$this -> msg[] =  $this -> setError('AlphaDigit', $methodRule[0]);
										
								} 
			
							break;
			
							case 'Range':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field, $methodRule[2]))
										$this -> msg[] =  $this -> setError('Range', $methodRule[0]);
	
								} 
			
							break;
			
							case 'Type':
			
								if(method_exists(get_class($this), $methodRule[1])) 
								{
						  
									if(!$this -> $methodRule[1]($field, $methodRule[2]))
										$this -> msg[] =  $this -> setError('Type', $methodRule[0]);
						  
								} 
			
							break;
		
						}
		  
					}
		 
				}
	
		}
  
	}
  
  
	protected function setError($errorMsg, $fieldname) 
	{
		return str_replace('%field%', $fieldname, $this -> lang -> $errorMsg);
	}

  
	protected function required($string) 
	{ 
		return (!is_null($string) && $string != false && $string != '') ? true : false;
	}
  
  
	protected function MinLenght($string, $min) 
	{  
      return (strlen($string) >= $min) ? true : false;  
	}
  

	protected function MaxLenght($string, $max) 
	{
		return (strlen($string) <= $max) ? true : false; 
	}
  

	protected function Between($string, $min, $max) 
	{
		return (strlen($string) >= $min && strlen($string) <= $max) ? true : false; 
	}
  
  
	protected function Email($email) 
	{
      return (preg_match('/^[a-zA-Z0-9\.\-\_]+\@[a-zA-Z0-9\.\-\_]+\.[a-z]{2,4}$/D', $email)) ? true : false;
	}
  
  
	protected function Ip($ip) 
	{
      $ex_ip = explode('.', $ip);
	  return (count($ex_ip) == 4) ? true : false;
	}
  
  
	protected function Regexp($string, $regexp) 
	{
		return (preg_match($regexp, $string)) ? true : false;
	}
  
  
	protected function Alpha($string) 
	{
		return (preg_match('/^[a-zA-Z\-\_\@]/', $string)) ? true : false;
	}
  
  
	protected function Digit($string) 
	{
		return (preg_match('/^[0-9]/', $string)) ? true : false;
	}
  
	protected function AlphaDigit($string) 
	{
		return (preg_match('/^[a-zA-Z0-9\-\_\@]/', $string)) ? true : false;
	}
  
  
	protected function Range($string, $range) 
	{ 
		return (preg_match('/^['.$range.']+/', $string)) ? true : false;
	}
  
  
	protected function Type($string, $type) 
	{
      $varType = gettype($string);
	  return ($type == $varType) ? true : false;
	}
  
  
	public function getValidationErrors($err = false) 
	{
		if($err === true and count($this -> msg) > 0)
			return implode('<Br>', $this -> msg);
	  
		return $this -> msg[0];
  	}
   
}

?> 